package adapter;

import java.util.ArrayList;

import com.adgroup.smartnote.AddMemberToGroupActivity;
import com.adgroup.smartnote.R;
import com.androidquery.AQuery;

import JsonModel.FamiliarFriend;
import JsonModel.User;
import JsonModel.UserLogo;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class AdapterChooseFriendToGroupMenu {
	
	private Context context;
	private ArrayList<FamiliarFriend> friends;

	public AdapterChooseFriendToGroupMenu(Context context, ArrayList<FamiliarFriend> friends) {
		this.context = context;
		this.friends = friends;		
	}
	public class Adapter extends BaseAdapter {
		@Override
		public int getCount() {
			return friends.size();
		}

		@Override
		public Object getItem(int arg0) {
			return friends.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}
		public void setFriends(ArrayList<FamiliarFriend> u)
		{
			friends.clear();
			friends = u;
		}
		@Override
		public View getView(final int arg0, View arg1, ViewGroup arg2) {
			Holder holder;
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			if (arg1 == null) {
				holder = new Holder();
				arg1 = inflater.inflate(R.layout.itemchosefriendtogroup, null);
				holder.iv = (ImageView) arg1
						.findViewById(R.id.imageViewLogoItemChooseFriendToGroupMenu);
				holder.tv = (TextView) arg1
						.findViewById(R.id.textViewNickNameItemChooseFriendToGroupMenu);
				holder.btn = (ImageButton) arg1.findViewById(R.id.imageButtonDeleteItemChooseFriendToGroupMenu);
				arg1.setTag(holder);
			} else {
				holder = (Holder) arg1.getTag();
			}
			AQuery aq = new AQuery(arg1);
			Bitmap placeholder = aq.getCachedImage(R.drawable.imageload);
			aq.id(holder.iv).image(friends.get(arg0).getLink(), true,
					true, 0, 0, placeholder, AQuery.FADE_IN_NETWORK, 1.0f);
			holder.tv.setText(friends.get(arg0).getNickName());
			holder.btn.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg) { 
					((AddMemberToGroupActivity)context).updateDeleteUser(friends.get(arg0));
					/*friends.remove(arg0);
					int n = friends.size();
					AdapterChooseFriendToGroupMenu.Adapter.this.notifyDataSetChanged();	*/				
				}
			});
			return arg1;
		}
	}

	public class Holder {
		ImageView iv;
		TextView tv;
		ImageButton btn;
	}
}
